﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DB.Model;
namespace KZ400B
{
    
    public class RunningUnitCacheManager
    {
        private static Dictionary<string, RunningUnit> s_ruList = new Dictionary<string, RunningUnit>();
        private static Dictionary<string, RunningUnit> s_ruByIdList = new Dictionary<string, RunningUnit>();
        private static int s_ruId = 0;
        
        public static void AddRunningUnit
            (DB.Model.RunningUnit ru)
        {
            if (!s_ruList.ContainsKey(ru.Name))
            {
                ru.ID = s_ruId;
                s_ruList.Add(ru.Name, ru);
                s_ruByIdList.Add(s_ruId.ToString(), ru);
                s_ruId++;
            }
        }
        

        public static RunningUnit getRunningUnit(string name)
        {
            if (s_ruList.ContainsKey(name))
            {
                return s_ruList[name];
            }
            return null;
        }



        public static RunningUnit getRunningUnit(int id)
        {
            return getRunningUnitById(id.ToString());
        }

        public static string getName(int id)
        {
            RunningUnit ru = getRunningUnit(id);
            if (ru != null)
            {
                return ru.Name;
            }
            return string.Empty;
        }

        public static string getAltName(int id)
        {
            RunningUnit ru = getRunningUnit(id);
            if (ru != null)
            {
                return ru.AltName;
            }
            return string.Empty;
        }

        public static RunningUnit getRunningUnitById(string id)
        {
            if (s_ruByIdList.ContainsKey(id))
            {
                return s_ruByIdList[id];
            }
            return null;
        }


        static DB.BLL.Axle m_aManager = new DB.BLL.Axle();
        /*
        public static void Update(Axle axle)
        {
            s_ruList[axle.Name] = axle;
            s_axleByIdList[axle.ID.ToString()] = axle;

            if (!m_aManager.Exists(axle.ID))
            {
                m_aManager.Add(axle.DBModel);
            }
            else
            {
                m_aManager.Update(axle.DBModel);
            }
        }
        */ 

        public static void InitFromDB()
        {
            DB.BLL.RunningUnit axManager = new DB.BLL.RunningUnit();
            List<DB.Model.RunningUnit> axList = null;
            try
            {
                axList = axManager.GetModelList(string.Empty);
            }
            catch (Exception ex)
            {
                axList = new List<DB.Model.RunningUnit>();
            }
            if (axList != null && axList.Count > 0)
            {
                foreach (DB.Model.RunningUnit aModel in axList)
                {
                    //RunningUnit ax = new RunningUnit(aModel);
                    AddRunningUnit(aModel);
                }
            }
            /*
            else
            {

                List<KZ400B.Axle> m_axleList = new List<KZ400B.Axle>();
                
                if (m_axleList.Count <= 0)
                {
                    m_axleList.Add(new KZ400B.Axle("X"));
                    m_axleList.Add(new KZ400B.Axle("Y"));
                    m_axleList.Add(new KZ400B.Axle("Z"));
                    m_axleList.Add(new KZ400B.Axle("T"));
                    m_axleList.Add(new KZ400B.Axle("U"));
                    m_axleList.Add(new KZ400B.Axle("W"));
                }
                foreach (KZ400B.Axle axle in m_axleList)
                {
                    AddAxle(axle);
                }
            }
             */ 
        }
    }
     
}
